<template>
  <div class="settlement-page">
    <h2>结算管理</h2>
    
    <!-- 统计卡片 -->
    <div class="stats-grid">
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon settlement">
            <el-icon><Money /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ formatMoney(stats.totalSettlement) }}</div>
            <div class="stat-label">总结算金额</div>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon count">
            <el-icon><Document /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ stats.totalCount }}</div>
            <div class="stat-label">结算订单数</div>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon pending">
            <el-icon><Clock /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ stats.pendingCount }}</div>
            <div class="stat-label">待结算订单</div>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon avg">
            <el-icon><TrendCharts /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ formatMoney(stats.avgSettlement) }}</div>
            <div class="stat-label">平均结算金额</div>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 搜索筛选 -->
    <el-card class="search-card">
      <el-form :model="searchForm" inline>
        <el-form-item label="订单号">
          <el-input v-model="searchForm.order_id" placeholder="请输入订单号" clearable />
        </el-form-item>
        <el-form-item label="结算时间">
          <el-date-picker
            v-model="searchForm.settlement_time"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="结算类型">
          <el-select v-model="searchForm.settlement_type" placeholder="请选择类型" clearable>
            <el-option label="正常结算" value="normal" />
            <el-option label="退款结算" value="refund" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 结算数据表格 -->
    <el-card class="table-card">
      <el-table :data="settlementList" style="width: 100%" v-loading="loading">
        <el-table-column prop="order_id" label="订单号" width="180" />
        <el-table-column prop="settlement_time" label="结算时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.settlement_time) }}
          </template>
        </el-table-column>
        <el-table-column prop="settlement_amount" label="结算金额" width="120">
          <template #default="{ row }">
            {{ formatMoney(row.settlement_amount) }}
          </template>
        </el-table-column>
        <el-table-column prop="settlement_type" label="结算类型" width="100">
          <template #default="{ row }">
            <el-tag :type="row.settlement_type === 'normal' ? 'success' : 'warning'">
              {{ row.settlement_type === 'normal' ? '正常结算' : '退款结算' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="settlement_account" label="结算账户" width="150" />
        <el-table-column prop="product_name" label="商品名称" />
        <el-table-column prop="product_quantity" label="数量" width="80" />
        <el-table-column prop="talent_name" label="达人" width="120" />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleView(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'

// 搜索表单
const searchForm = reactive({
  order_id: '',
  settlement_time: [],
  settlement_type: ''
})

// 分页信息
const pagination = reactive({
  current: 1,
  size: 20,
  total: 0
})

// 加载状态
const loading = ref(false)

// 统计数据
const stats = ref({
  totalSettlement: 123456.78,
  totalCount: 1234,
  pendingCount: 56,
  avgSettlement: 100.12
})

// 结算列表
const settlementList = ref([
  {
    order_id: 'DY202401010001',
    settlement_time: '2024-01-01 10:00:00',
    settlement_amount: 299.00,
    settlement_type: 'normal',
    settlement_account: '支付宝',
    product_name: '测试商品1',
    product_quantity: 1,
    talent_name: '测试达人1'
  },
  {
    order_id: 'DY202401010002',
    settlement_time: '2024-01-01 11:00:00',
    settlement_amount: 199.00,
    settlement_type: 'normal',
    settlement_account: '微信支付',
    product_name: '测试商品2',
    product_quantity: 2,
    talent_name: '测试达人2'
  },
  {
    order_id: 'DY202401010003',
    settlement_time: '2024-01-01 12:00:00',
    settlement_amount: 399.00,
    settlement_type: 'refund',
    settlement_account: '支付宝',
    product_name: '测试商品3',
    product_quantity: 1,
    talent_name: '测试达人3'
  }
])

// 搜索
const handleSearch = () => {
  pagination.current = 1
  fetchSettlementList()
}

// 重置
const handleReset = () => {
  Object.assign(searchForm, {
    order_id: '',
    settlement_time: [],
    settlement_type: ''
  })
  handleSearch()
}

// 查看详情
const handleView = (row: any) => {
  console.log('查看结算详情:', row)
}

// 分页大小改变
const handleSizeChange = (size: number) => {
  pagination.size = size
  fetchSettlementList()
}

// 当前页改变
const handleCurrentChange = (current: number) => {
  pagination.current = current
  fetchSettlementList()
}

// 获取结算列表
const fetchSettlementList = async () => {
  try {
    loading.value = true
    // TODO: 调用API获取结算列表
    pagination.total = 100
  } catch (error) {
    ElMessage.error('获取结算列表失败')
  } finally {
    loading.value = false
  }
}

// 格式化金额
const formatMoney = (amount: number) => {
  return `¥${amount.toFixed(2)}`
}

// 格式化日期
const formatDate = (date: string) => {
  return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
}

onMounted(() => {
  fetchSettlementList()
})
</script>

<style scoped>
.settlement-page {
  padding: 20px;
}

.settlement-page h2 {
  margin-bottom: 20px;
  color: #333;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.stat-card {
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.stat-content {
  display: flex;
  align-items: center;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  font-size: 24px;
  color: white;
}

.stat-icon.settlement {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stat-icon.count {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-icon.pending {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-icon.avg {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

.search-card {
  margin-bottom: 20px;
}

.table-card {
  margin-bottom: 20px;
}

.pagination-wrapper {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style> 